package com.library.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.library.dao.BookTitle;
import com.library.dao.BookTitleDAO;
import com.library.dao.DAOFactory;
import com.library.dao.MySQLDAOFactory;

/**
 * Servlet implementation class SearchBook
 */
@WebServlet("/SearchBook")
public class SearchBook extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchBook() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String publisher = request.getParameter("publisher");
		DAOFactory factory = new MySQLDAOFactory();
		BookTitleDAO dao = factory.getBookTitleDAO();
		ArrayList<BookTitle> list = dao.search(title, author, publisher);		
		response.setContentType("text/xml;charset=UTF-8");
	    PrintWriter out = response.getWriter();
	    
	    out.println("<?xml version=\"1.0\"?>");
	    out.println("<booktitle>");
		for (BookTitle b: list){
			out.println("<book>");
			out.println("<title>");
			out.println(b.getTitle());
			out.println("</title>");
			out.println("<author>");
			out.println(b.getAuthor());
			out.println("</author>");
			out.println("<publisher>");
			out.println(b.getPublisher());
			out.println("</publisher>");
			out.println("<description>");
			out.println(b.getDescription());
			out.println("</description>");
			out.println("<ID>");
			out.println(b.getBookTitleId());
			out.println("</ID>");
			out.println("</book>");
		}		
		out.println("</booktitle>")	;
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
